Vue.createApp({
    data() {
        return {
            time: '2022年11月19日 星期六 14:17',
            basicInfo: {
                name: '匿名',
                type: '神秘人',
                phone: '00000000000',
                blockName: 'group',
                groupName: 'block',
                blocks: [],
                groups: []
            },
            selectInfo: {
                groupName: 'null',
                blockName: 'null',
                type: 'takeaway',
                keyword: ''
            },
            tableData: [],
            selectedItem: []
        }
    },
    methods: {
        getTime() {
            const date = new Date();
            const year = date.getFullYear();
            let month = date.getMonth() + 1;
            month = month < 10 ? "0" + month : month;
            let day = date.getDate();
            day = day < 10 ? "0" + day : day;
            const week = date.getDay();
            let week_let = ["日", "一", "二", "三", "四", "五", "六"];
            let hour = date.getHours();
            hour = hour < 10 ? "0" + hour : hour;
            let minute = date.getMinutes();
            minute = minute < 10 ? "0" + minute : minute;
            this.time = year + "年" + month + "月" + day + "日 星期" + week_let[week] + " " + hour + ":" + minute;
        }
        ,
        showTopButton(type) {
            if (type === "sender") {
                document.getElementById("selectBlock").remove();
                document.getElementById("selectGroup").remove();
                document.getElementById("selectType").remove();
            } else if (type === "admin") {
                document.getElementById("liveGroup").remove();
                document.getElementById("selectBlock").remove();
                document.getElementById("adminTypeOption").remove();
                document.getElementById("blockTypeOption").remove();
            } else { // type == root
                document.getElementById("liveGroup").remove();
                document.getElementById("liveBlock").remove();
            }
        }
        ,
        deleteIt() {
            if (this.selectedItem.length <= 0) {
                alert("请选择后再删除！");
                return;
            }
            if (confirm("是否删除？")) {
                axios.post("/page/delete?type=" + this.selectInfo.type, this.selectedItem).then(resp => {
                    if (resp.data === "OK") {
                        alert("删除成功！");
                        this.getData();
                    } else {
                        let rs = resp.data.split(":");
                        if (rs.length > 1 && rs[1] === "Partial_Success") {
                            alert("部分删除成功！成功数：" + rs[2])
                            this.getData();
                        } else
                            alert("未知错误导致失败！");
                    }
                }).catch(ex => {
                    alert("未知错误导致失败！");
                });
            }
        }
        ,
        sendTakeaway() {
            if (this.selectedItem.length <= 0) {
                alert("请选择后再操作！");
                return;
            }
            if (confirm("是否修改送达状态？")) {
                axios.post("/page/send?type=takeaway", this.selectedItem).then(resp => {
                    if (resp.data === "OK") {
                        alert("修改送达状态成功！");
                        this.getData();
                    } else {
                        let rs = resp.data.split(":");
                        if (rs.length > 1 && rs[1] === "Partial_Success") {
                            alert("部分修改成功！成功数：" + rs[2])
                            this.getData();
                        } else
                            alert("未知错误导致失败！");
                    }
                }).catch(ex => {
                    alert("未知错误导致失败！");
                });
            }
        }
        ,
        changeGroupSelect() {
            axios.get("/page/get/group?blockName=" + this.selectInfo.blockName).then(resp => {
                this.basicInfo.groups = resp.data;
            });
        }
        ,
        changeTypeSelect() {
            this.tableData = [];
            this.selectedItem = [];
        },
        getData() {
            axios.get("/page/get/table", {
                params: this.selectInfo
            }).then(resp => {
                this.tableData = resp.data;
            });
        }
    },
    mounted() {
        setInterval(this.getTime(), 1000);

        axios.get("/page/get/basic").then(resp => {
            this.basicInfo = resp.data;
            this.showTopButton(resp.data.type);
        });


    }
}).mount("#page-app");